/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Productor;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Camp;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author David
 */
public class ProductorJpaController implements Serializable {

    public ProductorJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Productor productor) {
        if (productor.getCampCollection() == null) {
            productor.setCampCollection(new ArrayList<Camp>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Camp> attachedCampCollection = new ArrayList<Camp>();
            for (Camp campCollectionCampToAttach : productor.getCampCollection()) {
                campCollectionCampToAttach = em.getReference(campCollectionCampToAttach.getClass(), campCollectionCampToAttach.getId());
                attachedCampCollection.add(campCollectionCampToAttach);
            }
            productor.setCampCollection(attachedCampCollection);
            em.persist(productor);
            for (Camp campCollectionCamp : productor.getCampCollection()) {
                Productor oldProductorIdOfCampCollectionCamp = campCollectionCamp.getProductorId();
                campCollectionCamp.setProductorId(productor);
                campCollectionCamp = em.merge(campCollectionCamp);
                if (oldProductorIdOfCampCollectionCamp != null) {
                    oldProductorIdOfCampCollectionCamp.getCampCollection().remove(campCollectionCamp);
                    oldProductorIdOfCampCollectionCamp = em.merge(oldProductorIdOfCampCollectionCamp);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Productor productor) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Productor persistentProductor = em.find(Productor.class, productor.getId());
            Collection<Camp> campCollectionOld = persistentProductor.getCampCollection();
            Collection<Camp> campCollectionNew = productor.getCampCollection();
            List<String> illegalOrphanMessages = null;
            for (Camp campCollectionOldCamp : campCollectionOld) {
                if (!campCollectionNew.contains(campCollectionOldCamp)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Camp " + campCollectionOldCamp + " since its productorId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Camp> attachedCampCollectionNew = new ArrayList<Camp>();
            for (Camp campCollectionNewCampToAttach : campCollectionNew) {
                campCollectionNewCampToAttach = em.getReference(campCollectionNewCampToAttach.getClass(), campCollectionNewCampToAttach.getId());
                attachedCampCollectionNew.add(campCollectionNewCampToAttach);
            }
            campCollectionNew = attachedCampCollectionNew;
            productor.setCampCollection(campCollectionNew);
            productor = em.merge(productor);
            for (Camp campCollectionNewCamp : campCollectionNew) {
                if (!campCollectionOld.contains(campCollectionNewCamp)) {
                    Productor oldProductorIdOfCampCollectionNewCamp = campCollectionNewCamp.getProductorId();
                    campCollectionNewCamp.setProductorId(productor);
                    campCollectionNewCamp = em.merge(campCollectionNewCamp);
                    if (oldProductorIdOfCampCollectionNewCamp != null && !oldProductorIdOfCampCollectionNewCamp.equals(productor)) {
                        oldProductorIdOfCampCollectionNewCamp.getCampCollection().remove(campCollectionNewCamp);
                        oldProductorIdOfCampCollectionNewCamp = em.merge(oldProductorIdOfCampCollectionNewCamp);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = productor.getId();
                if (findProductor(id) == null) {
                    throw new NonexistentEntityException("The productor with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Productor productor;
            try {
                productor = em.getReference(Productor.class, id);
                productor.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The productor with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Camp> campCollectionOrphanCheck = productor.getCampCollection();
            for (Camp campCollectionOrphanCheckCamp : campCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Productor (" + productor + ") cannot be destroyed since the Camp " + campCollectionOrphanCheckCamp + " in its campCollection field has a non-nullable productorId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(productor);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Productor> findProductorEntities() {
        return findProductorEntities(true, -1, -1);
    }

    public List<Productor> findProductorEntities(int maxResults, int firstResult) {
        return findProductorEntities(false, maxResults, firstResult);
    }

    private List<Productor> findProductorEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Productor.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Productor findProductor(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Productor.class, id);
        } finally {
            em.close();
        }
    }

    public int getProductorCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Productor> rt = cq.from(Productor.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
